﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using mBlogAPI.Schema;
using mBlogAPI.BL;
using MyDesktopMicroBlog.Converter.StatusHelper;
using MyDesktopMicroBlog.Tabs;
using System.Windows.Media.Animation;

namespace MyDesktopMicroBlog
{
    public partial class MainWindow : BaseWindow
    {
        public Storyboard myStoryBoard = new Storyboard();
        public User CurrentUser { get; set; }

        private StatusHelper MyStatusHelper = new StatusHelper();
        public MainWindow()
        {
            StatusHelper.OnStatusUpdate += new TwoArgsDelegate<Status, bool>(InsertStatusToMainWindow);
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            InitializeComponent();
        }

        public void InsertStatusToMainWindow(Status status, bool result)
        {
            string statusText = "状态更新成功";
            if (result == true)
            {
                (frmBlogList.Content as BlogList).OStatusesCollection.Insert(0, status);
            }
            else
            {
                statusText = "状态更新失败，请重试";
            }
            MessageHelper.ShowAndHideMessage(statusText);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.txtStatus.DataContext = MyStatusHelper;
            InitUserInfo();
            InitFriendsInfo();
            InitPostInfo();
            InitProgressStoryBoard();
        }

        private void InitProgressStoryBoard()
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = 1;
            doubleAnimation.To = 0;
            doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));
            Storyboard.SetTargetName(doubleAnimation, tbStatus.Name);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(TextBlock.OpacityProperty));
            myStoryBoard.Children.Add(doubleAnimation);
        }

        private void InitFriendsInfo()
        {
        }

        private void InitPostInfo()
        {

        }

        private void InitUserInfo()
        {
            //init user information
            BitmapImage head = new BitmapImage();
            head.BeginInit();
            head.UriSource = new Uri(CurrentUser.ProfileImageUrl);
            head.EndInit();
            imgProfileImage.Source = head;
            lblName.Content = CurrentUser.ScreenName;
        }

        private void Min_Click(object sender, RoutedEventArgs e)
        {
            this.ShowInTaskbar = false;
            this.Hide();
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        #region Window Button Click
        private void Max_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == System.Windows.WindowState.Maximized)
            {
                this.WindowState = System.Windows.WindowState.Normal;
            }
            else
            {
                this.WindowState = System.Windows.WindowState.Maximized;
            }
        }

        private void BaseWindow_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.WindowState == System.Windows.WindowState.Maximized)
            {
                this.WindowState = System.Windows.WindowState.Normal;
            }
            else
            {
                this.WindowState = System.Windows.WindowState.Maximized;
            }
        }
        #endregion

        private void txtStatus_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                MessageHelper.ShowMessage("正在更新状态");
                if (string.IsNullOrWhiteSpace(txtStatus.Text.ToString()))
                {
                    MyStatusHelper.Status = StatusHelper.StatusProperty.DefaultMetadata.DefaultValue.ToString();
                }
            }
            catch
            {
                MessageHelper.ShowAndHideMessage("状态更新失败,请重试");
            }
        }

        private void txtStatus_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                mainTab.Focus();
            }
        }

        private void txtStatus_GotFocus(object sender, RoutedEventArgs e)
        {
            if (MyStatusHelper.Status == StatusHelper.StatusProperty.DefaultMetadata.DefaultValue.ToString())
            {
                MyStatusHelper.Status = "";
            }
        }

        private void txtStatus_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            txtStatus.SelectAll();
        }
    }
}
